package com.uchengwang.ui.cm_ui.ucity.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.MainActivity;
import com.uchengwang.ui.cm_ui.me.presenter.HttpUserManager;
import com.uchengwang.ui.cm_ui.ucity.cityBean.SchoolBean;
import com.uchengwang.ui.cm_ui.ucity.ui.SwitchSchoolActivity;
import com.uchengwang.ui.cm_ui.umsg.bean.ClubChooseBean;
import com.uchengwang.ui.utils.ActivityUtils;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.SPUtils;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by U城网 on 2017/4/7.
 * 选择院校
 */

public class ChooseSchoolAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<SchoolBean> list = new ArrayList<>();
    private List<ClubChooseBean> chooseBeenList = new ArrayList<>();
    private int type;//1选择学校 返回U城 刷新主页 3选择学校 将学校信息带回上个界面  2选择社团  4选择学校认证

    public ChooseSchoolAdapter(Context context, int type){
        this.context = context;
        this.type = type;
    }

    public void setData(List<SchoolBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<SchoolBean> getData(){
        return list;
    }

    public void setClubData(List<ClubChooseBean> list){
        chooseBeenList = list;
        notifyDataSetChanged();
    }

    public List<ClubChooseBean> getClubData(){
        return chooseBeenList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_choose_item, parent, false);
        return new ChooseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        ChooseViewHolder holder = (ChooseViewHolder) h;

        switch (type){
            case 1://选择学校 返回刷新U城主页
                final SchoolBean bean = list.get(position);
                final String name = bean.getUniName();
                holder.adapter_choose_text.setText(name);
                if (position !=0){
                    if (bean.isHavaData()){
                        holder.adapter_choose_tag.setVisibility(View.GONE);
                    }else {
                        holder.adapter_choose_tag.setVisibility(View.VISIBLE);
                        holder.adapter_choose_tag.setText("敬请期待...");
                    }
                }else {
                    holder.adapter_choose_tag.setVisibility(View.GONE);
                }


                holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (name.equals(UChengApplication.schoolName)){
                            return;
                        }

                        DialogUtils.getUtils().showDialogSingle(context, "确定切换到" + name + "?", "确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                DialogUtils.getUtils().cancel();

                                ActivityUtils.getUtils().clearMainActivity();
                                UChengApplication.uniId = bean.getId();
                                UChengApplication.schoolName = bean.getUniName();
                                Intent intent = new Intent(context, MainActivity.class);
                                context.startActivity(intent);
                            }
                        });

                    }
                });
                break;
            case 2://选择社团
                final ClubChooseBean chooseBean = chooseBeenList.get(position);
                final String orgName = chooseBean.getOrgName();
                holder.adapter_choose_text.setText(orgName);

                holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent();
                        intent.putExtra(BaseConfig.ORG_NAME, orgName);
                        intent.putExtra(BaseConfig.ORG_AUTH_ID, chooseBean.getId());
                        ((Activity)context).setResult(2, intent);
                        ((Activity)context).finish();

                    }
                });
                break;
            case 3://选择学校 将学校信息带回上界面

                final SchoolBean schoolBean =  list.get(position);
                final String schoolName = schoolBean.getUniName();
                holder.adapter_choose_text.setText(schoolName);

                holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent();
                        intent.putExtra(BaseConfig.UNI_NAME, schoolName);
                        intent.putExtra(BaseConfig.UNI_ID, schoolBean.getId());
                        ((Activity)context).setResult(2, intent);
                        ((Activity)context).finish();

                    }
                });
                break;
            case 4://选择学校 认证
                final SchoolBean authenBean =  list.get(position);
                final String uniName = authenBean.getUniName();
                holder.adapter_choose_text.setText(uniName);

                holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        DialogUtils.getUtils().showDialogSingle(context, "您已选择" + uniName + ", 选择后不可更改, 确定提交?", "确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                DialogUtils.getUtils().cancel();

                                MapRequest map = new MapRequest();
                                map.put(BaseConfig.IS_AUTH, 1);
                                map.put(BaseConfig.UNI_ID, authenBean.getId());
                                map.put(BaseConfig.UNI_NAME, uniName);
                                HttpUserManager.getManager().changeUserInfo((Activity) context, map, new OnResultListener() {
                                    @Override
                                    public void onResult(int stutas, String msg, int total, String data) {
                                        if (stutas ==0){
                                            UChengApplication.isAccountChange = true;
                                            UChengApplication.uniId = authenBean.getId();
                                            UChengApplication.myUniId = authenBean.getId();

                                            UChengApplication.schoolName = authenBean.getUniName();
                                            UChengApplication.mySchoolName = authenBean.getUniName();

                                            UChengApplication.isAuthen = true;
                                            UChengApplication.userRole = 1;

                                            SPUtils.getPreferences(context).putBoolean(BaseConfig.IS_AUTHEN, true);
                                            SPUtils.getPreferences(context).putLong(BaseConfig.UNI_ID, authenBean.getId());
                                            SPUtils.getPreferences(context).putString(BaseConfig.UNI_NAME, authenBean.getUniName());


                                            ((SwitchSchoolActivity)context).toMain();

//                                            Intent intent = new Intent(context, MainActivity.class);
//                                            intent.putExtra(BaseConfig.INDEX, 3);
//                                            context.startActivity(intent);
                                        }else {
                                            UCommonUtil.getUtil().showToast(context, msg);
                                        }
                                    }
                                });
                            }
                        });

                    }
                });
                break;
        }




    }

    @Override
    public int getItemCount() {
        if (type ==1 || type ==3 || type ==4){
            return list.size();
        }else if (type ==2){
            return chooseBeenList.size();
        }

        return 0;
    }

    class ChooseViewHolder extends RecyclerView.ViewHolder{
        private TextView adapter_choose_text, adapter_choose_tag;
        private RelativeLayout linearLayout;

        public ChooseViewHolder(View view) {
            super(view);

            AutoUtils.autoSize(view);

            linearLayout = (RelativeLayout) view;
            adapter_choose_text = (TextView) view.findViewById(R.id.adapter_choose_text);
            adapter_choose_tag = (TextView) view.findViewById(R.id.adapter_choose_tag);
        }
    }
}
